package com.xsm.ppsecret.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xsm.ppsecret.R;
import com.xsm.ppsecret.model.PwdEntry;

import java.util.List;

/**
 * Author: Xiasem
 * Email: xiasem@163.com
 * Date: 17-4-7.
 */

public class PwdAdapter extends BaseRecyclerAdapter implements View.OnClickListener, View.OnLongClickListener {

    private Context mContext;
    private List<PwdEntry> mList;

    public PwdAdapter(Context context, List<PwdEntry> list) {
        mContext = context;
        mList = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pwd_layout, parent, false);
        view.setOnClickListener(this);
        view.setOnLongClickListener(this);
        return new PwdHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof PwdHolder) {
            PwdHolder pwdHolder = (PwdHolder) holder;
            pwdHolder.bindView(mList.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return mList.size() > 0 ? mList.size() : 0;
    }

    @Override
    public void onClick(View v) {
        super.adapterClick(v);
    }

    @Override
    public boolean onLongClick(View v) {
        super.adapterLongClick(v);
        return true;
    }

    private static class PwdHolder extends RecyclerView.ViewHolder {

        private TextView title;
        private TextView remark;

        PwdHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.item_pwd_title);
            remark = (TextView) itemView.findViewById(R.id.item_pwd_remark);
        }

        void bindView(PwdEntry entry) {
            String title = entry.getTitle();
            if (title != null) {
                this.title.setText(title);
            } else {
                this.title.setText("");
            }
            String remarks = entry.getRemarks();
            if (remarks != null && !remarks.isEmpty()) {
                this.remark.setVisibility(View.VISIBLE);
                this.remark.setText(remarks);
            } else {
                this.remark.setVisibility(View.GONE);
            }
        }

    }

}
